/****************************************************************************
** @brief concrete state subcalsses
****************************************************************************/

#include "idleStatus.h"
#include "readyStatus.h"
#include "chargingStatus.h"
#include "finishStatus.h"
#include "faultStatus.h"

#include "../hciMain.h"
#include "../cardMain.h"
#include "../oiovMain.h"
#include "../oiovCallback.h"
#include "../controllerMain.h"

#include "../../controller/controller_can.h"

#include "QDebug"
#define IDLESTATE_DEBUG

Status *IdleStatus::instance_ = nullptr;

IdleStatus::IdleStatus() : Status()
{
    state_ = Status::kIdle;
}

IdleStatus::~IdleStatus()
{

}

Status *IdleStatus::instance()
{
    if (nullptr == instance_) {
        instance_ = new IdleStatus();
    }
    return instance_;
}

void IdleStatus::Process(Gun *g)
{
    unsigned char no = g->gun_number();

    if ((CONGUNNOA != no) && (CONGUNNOB != no)) {
        return;
    }

    no = no - CONGUNNOA;

#if defined(IDLESTATE_DEBUG)
    static int count = 0;
    if (++count >= 5 * 1000 / 100) {
        count = 0;
        qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "idle state...";
        ControllerParserMain::con_telecommand[no].connect_bus = 0;
        ControllerParserMain::con_telecommand[no].work_state = 0;
    }
#endif

    switch (ControllerParserMain::con_telecommand[no].work_state) {
    case WAIT_STATE:
        //连接到车辆
        if (0 == ControllerParserMain::con_telecommand[no].connect_bus) {
            HciMain::ClearStartChargeService(no); //清除标志位
            HciMain::ClearStopChargeService(no); //清除标志位
            CardMain::ClearStartChargeService(no); //清除标志位
            CardMain::ClearStopChargeService(no); //清除标志位
            OiovReqMain::ClearAppStartChargeService(no); //清除标志位
            OiovReqMain::ClearAppStopChargeService(no); //清除标志位
            OiovReqMain::ClearCardAuthStartChargeService(no); //清除标志位
            ControllerParserMain::ClearStopChargingService(no); //清除标志位

            ChangeState(g, ReadyStatus::instance()); //切换到插枪准备

#if defined(IDLESTATE_DEBUG)
            qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "idle -> ready";
#endif
            //设备状态 -> 准备中
            memset(OiovCallback::ops_property_charge_data[no].serial_number, 0x00, sizeof(OiovCallback::ops_property_charge_data[no].serial_number));
            OiovCallback::ops_property_charge_data[no].status = OiovCallback::OPS_CHARGINGPILE_STATE_IDLE;
            OiovCallback::ops_property_charge_data[no].connect_gun = OiovCallback::OPS_GUN_CONNECT_STATE_YES;
            OiovCallback::ops_property_charge_data[no].output_voltage = 0x00;
            OiovCallback::ops_property_charge_data[no].output_current = 0x00;
            OiovCallback::ops_property_charge_data[no].line_temp = 0x00;
            OiovCallback::ops_property_charge_data[no].soc = 0x00;
            OiovCallback::ops_property_charge_data[no].battery_max_temp = 0x00;
            OiovCallback::ops_property_charge_data[no].charging_time = 0x00;
            OiovCallback::ops_property_charge_data[no].remaining_charging_time = 0x00;
            OiovCallback::ops_property_charge_data[no].charging_elect = 0x00;
            OiovCallback::ops_property_charge_data[no].loss_calculation_elect = OiovCallback::ops_property_charge_data[0].charging_elect;
            OiovCallback::ops_property_charge_data[no].charging_fee = 0x00;
            OiovCallback::ops_property_charge_data[no].fault = 0x00;
            //向平台发送设备状态事件
            OiovReqMain::SetDeviceStatusEvent(no);
        } else {
            //...
        }
        break;
    case CONF_STATE:
        break;
    case CHARGING_STATE:
        break;
    case FULL_STATE:
        break;
    case WARRING_STATE:
        break;
    case FAULT_STATE:
        ChangeState(g, FaultStatus::instance()); //切换到故障

#if defined(IDLESTATE_DEBUG)
        qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "idle -> fault";
#endif

        //设备状态 -> 故障中
        memset(OiovCallback::ops_property_charge_data[no].serial_number, 0x00, sizeof(OiovCallback::ops_property_charge_data[no].serial_number));
        OiovCallback::ops_property_charge_data[no].status = OiovCallback::OPS_CHARGINGPILE_STATE_FAULT;
        OiovCallback::ops_property_charge_data[no].connect_gun = OiovCallback::OPS_GUN_CONNECT_STATE_YES;
        OiovCallback::ops_property_charge_data[no].output_voltage = 0x00;
        OiovCallback::ops_property_charge_data[no].output_current = 0x00;
        OiovCallback::ops_property_charge_data[no].line_temp = 0x00;
        OiovCallback::ops_property_charge_data[no].soc = 0x00;
        OiovCallback::ops_property_charge_data[no].battery_max_temp = 0x00;
        OiovCallback::ops_property_charge_data[no].charging_time = 0x00;
        OiovCallback::ops_property_charge_data[no].remaining_charging_time = 0x00;
        OiovCallback::ops_property_charge_data[no].charging_elect = 0x00;
        OiovCallback::ops_property_charge_data[no].loss_calculation_elect = OiovCallback::ops_property_charge_data[no].charging_elect;
        OiovCallback::ops_property_charge_data[no].charging_fee = 0x00;
        OiovCallback::ops_property_charge_data[no].fault = 0x00;
        //向平台发送设备状态事件
        OiovReqMain::SetDeviceStatusEvent(no);
        break;
    default:
        break;
    }
}
